package com.framework.web.bus.controller;


import com.framework.core.domain.AjaxJson;
import com.framework.core.easyui.domain.DataGridModel;
import com.framework.core.easyui.util.PageUtil;
import com.framework.core.util.MyBeanUtils;
import com.framework.web.bus.domain.Cookbook;
import com.framework.web.bus.service.CookbookService;
import com.framework.web.common.domain.BusinessConstants;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author fusf
 * @version V1.0
 * @Title: Controller
 * @Description: 控制层
 * @date 2016-09-04
 */
@Controller
@RequestMapping("/cookbook")
public class CookbookController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());


    /**
     * Spring注入资源信息模块service
     */
    @Autowired
    private CookbookService cookbookService;


    /**
     * 分页
     *
     * @param dgm
     * @param cookbook
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> list(DataGridModel dgm, Cookbook cookbook, HttpServletRequest request) throws Exception {
        PageRequest pr = DataGridModel.getPageRequest(dgm);
        return PageUtil.getPageMap(cookbookService.getPageList(cookbook, pr));
    }

    /**
     * 请求新增或修改页面
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/toSaveAndUpdate/{id}", method = RequestMethod.GET)
    public ModelAndView toSaveAndUpdate(@PathVariable("id") String id, HttpServletRequest request)
            throws Exception {
        ModelAndView modelAndView = new ModelAndView("/bus/cookbook/cookbookForm");
        Cookbook cookbook = new Cookbook();
        if (StringUtils.isNotEmpty(id) && !"0".equals(id)) {
            cookbook = cookbookService.findById(id);
        } else {

        }
        if (cookbook.getImg() != null) {
            String[] StringArr = cookbook.getImg().split(",");
            modelAndView.addObject("StringArr", StringArr);
        }
        String realPath = "/jslib/img/cookbook/";
        modelAndView.addObject("cookbook", cookbook);
        modelAndView.addObject("realPath", realPath);

        return modelAndView;
    }


    /**
     * 保存信息
     *
     * @param cookbook 信息实体
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/saveAndUpdate", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson saveAndUpdate(HttpServletRequest request, HttpServletResponse response,
                                  Cookbook cookbook) throws Exception {
        AjaxJson j = new AjaxJson();
        if (StringUtils.isNotEmpty(cookbook.getId())) {//更新
            Cookbook r = cookbookService.findById(cookbook.getId());
            r.setImg(cookbook.getImg());
            MyBeanUtils.copyBeanNotNull2Bean(cookbook, r);
            if(cookbook.getImg()==null||"".equals(cookbook.getImg())){
                r.setImg("");//图片都删除时,设为空字符串
            }
            cookbook = r;
        } else {

        }
        /*初始化参数*/
        cookbook.setStatus(BusinessConstants.STATUS.NORMAL);
        cookbookService.save(cookbook);

        j.setMsg("保存成功");
        return j;
    }

    /**
     * 根据id字符串删除数据信息
     *
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson delete(String id) throws Exception {
        AjaxJson j = new AjaxJson();
        cookbookService.delete(id);
        j.setMsg("删除成功");
        return j;
    }



}